package com.ws.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.bean.Acl_user;
import com.ws.bean.User;
import com.ws.bean.vo.Acl_userVo;
import com.ws.service.Acl_UserService;
import com.ws.utils.Res;
import com.ws.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/users")
public class Acl_UserController {
    @Autowired
    private Acl_UserService aclUserService;

    //后台用户的查询
    @RequestMapping("list")
    public String tolist(Model model, Acl_userVo acl_userVo){

        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(acl_userVo.getCurrentPage(),acl_userVo.getPageSize());
        List<Acl_user> list = aclUserService.selAcl_user(acl_userVo);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("acl_userVo",acl_userVo);
        return "admin/user/list";
    }

    //添加
    @RequestMapping("toAdd")
    public String toAdd(){
        return "admin/user/add";
    }

    @RequestMapping("add")
    public String add(Acl_user aclUser, MultipartFile image) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            aclUser.setUser_tx(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));
            aclUser.setPassword(SecurityUtils.encrypt(aclUser.getPassword()));
        }
        aclUser.setPassword(SecurityUtils.encrypt("12345"));
        aclUserService.add(aclUser);
        return "redirect:/users/list";
    }

    //删除
    @RequestMapping("delete")
    public String delete(Integer user_id,String tx){
        File file = new File(Res.fileDir+tx);
        if (file.exists()){
            file.delete();
        }
        int i =  aclUserService.delete(user_id);
        return "redirect:/users/list";
    }
    //修改
    @RequestMapping("toUpdate")
    public String toUpdate(Integer user_id,Model model){
        Acl_user aclUser = aclUserService.selAcl_userOne(user_id);
        model.addAttribute("aclUser",aclUser);
        return "admin/user/update";
    }
    @RequestMapping("update")
    public String update(Acl_user aclUser, MultipartFile image,String tx) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            aclUser.setUser_tx(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));

            Acl_user olduser = aclUserService.selAcl_userOne(aclUser.getUser_id());
            String oldTx = olduser.getUser_tx();

            if (oldTx!=null&&!oldTx.equals(aclUser.getUser_tx())){
                File oldFile = new File(Res.fileDir+oldTx);
                if (oldFile.exists()){
                    oldFile.delete();
                }
            }
        }else{
            aclUser.setUser_tx(tx);
        }
        aclUser.setPassword(SecurityUtils.encrypt(aclUser.getPassword()));
        int i =  aclUserService.update(aclUser);

        return "redirect:/users/list";
    }

    public Acl_user login(String username) {
        return aclUserService.login(username);
    }

}
